import {
	defineStore
} from 'pinia';
import {
	toLogin,
	toRegister,
	toUpdateHeadImg,
	toUpdateMessage,
	toGetCount

} from "@/utils/request/user.js"
export const useUserStore = defineStore('user', {
	state: () => {
		return {
			status: {
				code: "",
				msg: ""
			},
			total: {},
			myUnusedList: [],
			myConfessionList: [],
			myLostList: [],
			schoolList: [],
			user: {}

		};
	},
	// 也可以这样定义
	// state: () => ({ count: 0 })
	actions: {
		//登录
		async login(data) {
			try {
				const res = await toLogin(data);
				this.status.code = res.code;
				this.status.msg = res.msg;
				this.user = res.data
			} catch (err) {
				this.msg.code = "405",
					this.msg.msg = "网络不好，请稍后重试"
			}
		},
		async register(filePath, data) {
			try {
				const res = await toRegister(filePath, data);
				this.status.code = res.code;
				this.status.msg = res.msg
				if (res.code == "200") {
					this.user = res.data
				}
			} catch (err) {
				this.status.code = "405",
					this.status.msg = "网络不好，请稍后重试"
			}
		},
		async updateHeadImg(filePath, data) {
			try {
				const res = await toUpdateHeadImg(filePath, data);
				this.status.code = res.code;
				this.status.msg = res.msg
				if (res.code == "200") {
					this.user = res.data
				}
			} catch (err) {
				this.status.code = "405",
					this.status.msg = "网络不好，请稍后重试"
			}
		},
		async updateMessage(data) {
			try {
				const res = await toUpdateMessage(data);
				this.status.code = res.code;
				this.status.msg = res.msg
				if (res.code == "200") {
					this.user = res.data
				}
			} catch (err) {
				this.status.code = "405",
					this.status.msg = "网络不好，请稍后重试"
			}
		},
		async getCount(schoolId) {
			try {
				const res = await toGetCount(schoolId);
				this.status.code = res.code;
				this.status.msg = res.msg
				if (res.code == "200") {
					this.total = res.data
				}
			} catch (err) {
				this.status.code = "405",
					this.status.msg = "网络不好，请稍后重试"
			}
		}
	}
});